import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: '登录页'
    }
  },
  {
    path: '/home',
    name: 'home',
    redirect: '/welcome',
    component: () => import('../views/home'),
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('../views/main/welcome.vue'),
        meta: {
          title: '欢迎'
        }
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('../views/main/users.vue'),
        meta: {
          title: '用户列表'
        }
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../views/main/rights.vue'),
        meta: {
          title: '权限列表'
        }
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/main/roles.vue'),
        meta: {
          title: '角色列表'
        }
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../views/main/categories.vue'),
        meta: {
          title: '商品分类'
        }
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../views/main/params.vue'),
        meta: {
          title: '分类参数'
        }
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/main/goods.vue'),
        meta: {
          title: '商品列表'
        }
      },
      {
        path: '/goods/add',
        name: 'add',
        component: () => import('../views/main/add.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/main/orders.vue'),
        meta: {
          title: '订单列表'
        }
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('../views/main/reports.vue'),
        meta: {
          title: '数据报表'
        }
      }
    ]
  },
  {
    path: '*',
    name: 'err',
    component: () => import('../views/err/404.vue'),
  }
]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => { //路由全局守卫
  // let list = ['home', 'orders', 'caregories', 'reports', 'users', 'welcome','goods'] //模拟后端传过来的权限列表
  //获取token
  document.title = to.meta.title || '后台'
  const tokenStr = window.sessionStorage.getItem('token');
  //访问登录页并且token存在时跳转到首页
  if (to.path === '/login') {
    if (tokenStr) {
      return next({ path: '/welcome', replace: true })
    }
    next() //没有token访问登录页直接放行
  } else { //访问其他页面
    if (!tokenStr) { //token不存在时，不允许访问页面
      next({ path: '/login', query: { redirect: to.fullPath } })
    } else { //token存在时判断权限，不包含直接跳转到登录页
      // if (list.indexOf(to.name) === -1) {
      //   alert('没权限，滚出克')
      //   next('/login')
      // }
    }
  }
  next();
})

export default router
